/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Image,
  FlatList

} from 'react-native';
// import Bananas from './Component/Bananas.js'
// import BlinkText from './Component/BlinkText.js'
import jsonData from './mock.json'
const mockData = [
  { title: 'Title', year: '2015', posters: { thumbnail: 'http://i.imgur.com/UePbdph.jpg' } },
]
class project extends Component {
  constructor(props) {
    super(props)
    this.state = {
      movies: null,
      loaded: false
    }
  }
  fetchData() {
    setTimeout(() => {
      this.setState({
        movies: jsonData,
        loaded: true,
      })
    }, 1000)
  }
  // i => {
  //     i.movies = jsonData
  //     i.loaded = true
  //   }
  loading() {
    return (
      <View style={styles.container}>
        <Text>
          Loading movies...
        </Text>
      </View>
    )
  }
  renderMovie(movie) {
    return (
      <View style={styles.container} >
        <Image
          source={{ uri: movie.posters.thumbnail }}
          style={styles.thumbnail}
        />
        <View style={styles.rightContainer}>
          <Text style={styles.title}>{movie.title}</Text>
          <Text style={styles.year}>{movie.year}</Text>
        </View>
      </View>
    )
  }
  componentDidMount() {
    this.fetchData()
  }
  render() {
    return (
      this.state.loaded
        ?
        <FlatList
          data={this.state.movies.data}
          style={styles.listView}
          renderItem={({ item }) => this.renderMovie(item)}
          keyExtractor={item => item.title}
        />
        :
        this.loading()
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: 22,
    justifyContent: 'center',
    alignItems: 'center',
    // flexDirection: 'row',
    backgroundColor: '#F5FCFF',
  },
  thumbnail: {
    width: 53,
    height: 81,

  },
  right: {
    flex: 1
  },
  title: {
    fontSize: 20,
    marginBottom: 8,
    textAlign: 'center',
  },
  year: {
    textAlign: 'center',
  },
  listView: {
    // flex: 1,
    // backgroundColor: '#05a5d1'
  }
});

AppRegistry.registerComponent('project', () => project);
